/*
 * Monkey-Moore - A simple and powerful relative search tool
 * Copyright (C) 2007 Ricardo J. Ricken (Darkl0rd)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef MONKEY_FRAME_H
#define MONKEY_FRAME_H

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "constants.hpp"
#include "monkey_moore.hpp"
#include "monkey_prefs.hpp"

#include <wx/imaglist.h>
#include <vector>
#include <utility>

typedef std::pair <wxFileOffset, equivalency_type> mm_relative_type;
typedef std::pair <mm_relative_type, wxString> result_type;

/**
* Implements the main window frame. Creates and maintains the user interface,
* interacting and responding to the user's actions.
*/
class MonkeyFrame : public wxFrame
{
public:
   MonkeyFrame (const wxString &title, MonkeyPrefs &mprefs, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize);
   virtual ~MonkeyFrame ();

   void OnBrowse (wxCommandEvent &event);
   void OnSearchType (wxCommandEvent &event);
   void OnSearch (wxCommandEvent &event);
   void OnUseWC (wxCommandEvent &event);
   void OnAdvanced (wxCommandEvent &event);
   void OnEnableCP (wxCommandEvent &event);
   void OnCharsetList (wxCommandEvent &event);
   void OnCharsetChosen (wxCommandEvent &event);
   void OnAllResults (wxCommandEvent &event);
   void OnCreateTbl (wxCommandEvent &event);
   void OnClear (wxCommandEvent &event);
   void OnOptions (wxCommandEvent &event);
   void OnAbout (wxCommandEvent &event);
   void OnCancel (wxCommandEvent &event);
   void OnTextEnter (wxCommandEvent &event);
   void OnClose (wxCloseEvent &event);

   void OnJobFinished (wxCommandEvent &event);
   void OnJobAborted (wxCommandEvent &event);

   /**
   * Sets the progress bar percentage.
   * @param percentage a value in the 0-100 range
   */
   inline void SetCurrentProgress (const int percentage) const {
      GetWindow <wxGauge *> (MonkeyMoore_Progress)->SetValue(percentage);
   }

   /**
   * Finds out whether the search was aborted or not.
   * @return If true, it was aborted.
   */
   inline bool IsSearchAborted () const {
      return search_was_aborted;
   }

   /**
   * Shows a simple message box.
   * @param message content
   * @param caption optional caption
   */
   inline void ShowWarning (const wxString &message, const wxString &caption = _("Monkey-Moore - Error")) {
      wxMessageBox(message, caption, wxOK | wxICON_INFORMATION, this);
   }

private:
   void SetUIToBusy (const bool busy = true) const;
   void ShowProgressBar (const bool show = true);
   void CreateTable (const equivalency_type &d);
   bool CheckKeyword (const wxString &kw, const wxChar wc, const wxString &cp);
   void ShowResults (const std::vector <result_type> &r);

   /**
   * Finds the widget represented by some given id and return it
   * properly casted to the type specified in the template.
   * @param id widget's identifier
   * @return Pointer to the desired widget, properly casted.
   */
   template <class T> inline T GetWindow (const long id) const {
      return dynamic_cast <T> (FindWindowById(id));
   }

   std::vector <result_type> remove_duplicates (std::vector <result_type> &org);

   bool advanced_shown;
   bool search_done;
   bool search_in_progress;
   bool search_was_aborted;
   std::vector <result_type> last_results;
   wxImageList *images;
   wxMutex mutex;
   wxStopWatch chronometer;
   MonkeyPrefs &prefs;

   DECLARE_EVENT_TABLE();
};

#endif //~MONKEY_FRAME_H
